package com.example.demo.utils;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;

import java.util.Base64;
import java.util.Properties;

public class GetData {

    public static void main(String[] args) {

        String str = "12341234nacostokenwentf774977490";
        System.out.println(Base64.getEncoder().encodeToString(str.getBytes()));
        System.out.println(getByteLength());

        test();
    }

    public static int getByteLength() {
        String text = "汉字ABC汉字";
        int chineseCharacterCount = 0;

        for (int i = 0; i < text.length(); ) {
            int codePoint = text.codePointAt(i);
            if (Character.UnicodeBlock.of(codePoint) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
                chineseCharacterCount++;
                // 跳过字符，对于Supplementary Characters需要跳过两个char的索引
                i += Character.charCount(codePoint);
            } else {
                // 非汉字字符，跳过一个char的索引
                i++;
            }
        }

        System.out.println("汉字的数量: " + chineseCharacterCount);
        return chineseCharacterCount;
    }

    public static void test(){
        try {
            // Initialize the configuration service, and the console automatically obtains the following parameters through the sample code.
            String serverAddr = "127.0.0.1:8848";
            Properties properties = new Properties();
            properties.put("serverAddr", serverAddr);

            // if need username and password to login
            properties.put("username","nacos");
            properties.put("password","nacos");

            ConfigService configService = NacosFactory.createConfigService(properties);
            NamingService namingService = NacosFactory.createNamingService(properties);

            String content = configService.getConfig("springstudy.properties", "DEFAULT_GROUP", 5000);
            System.out.println("nacos的配置内容是"+content);

            System.out.println(configService);
        } catch (NacosException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("异常--------");
        }
    }
}
